var deptName="";
var date=echartUtil.GetDateStr(0);
var machining="";
var tableReportDetail;
//var spanValue=5;
//相关业务逻辑
$(function(){
	page.bindTag();
	page.getFactoryTurnover();
	nameToIdUtil.getProductionDepartment("productionDepartment");
	
});

var tableReport;
var page={//页面总变量
		bindTag:function(){
			$("#turnoverTable").bind("change",function(){
				deptName=$(this).val();
				page.getMachining(deptName);
			});
			
			
		    $("#queryTurnoverBtn").bind("click",function(){
		    	machining=$("#Machining option:selected").val();
		    	if(deptName==''){
		    		layAlert("请选择车间");
		    		return false;
		    	}
		    	page.timesDo();
		    });
		    
		    $("#productionDepartment").bind("change",function(){
				var deptId=$("#productionDepartment").val();
				page.getFactoryTurnover();
				page.getDept(deptId);
			});
		    
		     
		    $('#capacityTable tbody').on('click', 'tr', function () {
		    	var table = $('#capacityTable').DataTable();
		        var data = table.row( this ).data();
		        page.seeDetail(data.workShopId,data.orderId);
		    } );
		},
		timesDo:function(){
			page.getTrunover(deptName,date,machining);
	    	echartUtil.getManager(deptName);
	    	page.getDeptTurnover(deptName,date);
		},
		serverTurnover:function(deptName){
			ajaxUtil.ajaxRuquestNoQuan('workshop/serverTurnover',{'workShop':deptName,'machining':machining},function(data){
				var workShopArray=new Array();
				var workDataArray=new Array();
				for(var i=0;i<data.length;i++){
					workDataArray.push(data[i].reach);
					workShopArray.push(data[i].deptName);
				}
				echartUtil.barChartTemp('serverTurnoverDiv','七日周转量看板',['周转量'],workShopArray,workDataArray,'');
			});
		},
		getDept:function(deptId){
			nameToIdUtil.getWorkShopNameAndId("turnoverTable",deptId);
		},
		excleBind:function(){
			$("#downlodeExcle").bind("click",function(){
				var url="workshop/downlodeTurnover?workShop="+deptName+"&machining="+machining;
				tableUtil.download(url);
			});
		},
		getDeptTurnover:function(deptName,date){
			$.ajax({
				url:'workshop/getDeptTurnover',
				data:{
					'workShop':deptName,
					'date':	date,
					'machining':machining
				},
				success:function(data){
					if(data.result){
						data=data.data;
					    var html="<h4>";
						for(var i=0;i<data.length;i++){
							html+="<span class='kuang'>"+data[i].machining+""+data[i].turnover+
							"</span> &nbsp;&nbsp;&nbsp;";
						}
						html+="<button class='btn btn-success' id='downlodeExcle'>导出为EXCLE</button></h4>";
						$("#deptTurnover").html(html);
						page.excleBind()
					}else{
						layAlert(data.info);
						$("#deptTurnover").html("");
					}
				}
			});
		},
		getTrunover:function(workShop,date,machining){
			$("#divbody").show();
			tableReport=ajaxUtil.dataTablesAjaxNotFxiaction("capacityTable",10,'workshop/getTrunoverNew',
					{
				     'workShop':workShop,
				     'machining':machining},[
					{"data":"orderId"},
					{"data":"companyOrderId"},
					{"data":"factoryId"},
					{"data":"customerId"},
					{"data":"customerOrderId"},
					{"data":"machining"},
					{"data":"setTime",render: function (data,type,row){
						if(data<date && data!=''){
						   return "<span style='color:red'>"+data+"</span>"
						}else{
						   return data;
						}
					}},
					{"data":"inTime"},
					{"data":"inCount"},
					{"data":"outCount"},
					{"data":"setTime",render: function (data,type,row){
						console.log(row);
						return row.inCount - row.outCount ;
					}}
			]);
			page.serverTurnover(deptName);
//			var index = layer.load(0, {shade: false});
//			$.ajax({
//				url:'workshop/getTrunover',
//				data:{
//					'workShop':workShop,
//					'machining':machining
//				},
//				success:function(data){
//					layer.close(index);
//					if(data.result){
//						data=data.data;
//						if(data.length<=0){
//							layAlert("该车间没有正在生产的订单");
//						}else{
//							var html="<table id='turnoverTableDetail' class='table table-border table-bordered table-bg '><thead><tr><th rowspan='2' style='width:120px'>订单编号</th><th rowspan='2' style='width:90px'>工厂型号</th><th rowspan='2' style='width:90px'>客户型号</th><th rowspan='2' style='width:90px'>客户订单号</th>";
//						    var tital=data[data.length-1]["procedureMap"];
//						    for(var i=0;i<tital.length;i++){
//						    	html+="<th colspan='3' class='span'>"+tital[i]+"</th>";
//						    }
//						    html+="</tr><tr>"
//				    	   for(var i=0;i<tital.length;i++){
//						    	html+="<th class='kuang'>未完成</th><th class='kuang'>不良品</th><th style='width:80px'>报工时间</th>";
//						    }
//						    html+="</tr></thead><tbody>";
//						    var sumHtml="";
//						    for(var e=0;e<data.length-1;e++){
//						    	var bodyData=data[e];
//						    	html+="<tr><td><a class='order'>"+bodyData.orderId+
//						    	"</a></td><td>"+bodyData.factoryId+
//						    	"</td><td>"+bodyData.keHu+
//						    	"</td><td>"+bodyData.keHuOrder+"</td>";
//						    	for(var i=0;i<tital.length;i++){
//						    		var turnover=tital[i]+"turnover";
//						    		var noQualified=tital[i]+"noQualified";
//						    		var lastTime=tital[i]+"lastTime";
//							    	html+="<td class='kuang'>"+bodyData[turnover]+"</td>"
//							    	        +"<td class='kuang'>"+bodyData[noQualified]+"</td>"
//							    	        +"<td>"+bodyData[lastTime]+"</td>";
//							    }
//						    	html+="</tr>";
//						    }
//						    html+="</tbody></table>";
//						    $(".tableData").html("");
//						    $(".tableData").append(html);
////						    $('#turnoverTable').DataTable();
//						    aBindUtil.aOrderBind();
//							page.serverTurnover(deptName);
//						}
//					}else{
//						layAlert(data.info);
//						$(".tableData").html("");
//						$(".serverTurnoverDiv").html("");
//					}
//				
//			    tableUtil.dataTablesStaticUtil("turnoverTableDetail",4);
//				}
//			});
		},
		seeDetail:function(workShop,orderId){
		 	tableReportDetail=ajaxUtil.dataTablesAjaxNotFxiaction("capacityDetailTable",10,'workshop/getTrunoverNewDetail',
					{
				     'workShop':workShop,
				     'detail':1,
				     'orderId':orderId
				     },[
					{"data":"orderId"},
					{"data":"companyOrderId"},
					{"data":"factoryId"},
					{"data":"customerId"},
					{"data":"customerOrderId"},
					{"data":"machining"},
					{"data":"setTime",render: function (data,type,row){
						if(data<date && data!=''){
						   return "<span style='color:red'>"+data+"</span>"
						}else{
						   return data;
						}
					}},
					{"data":"color"},
					{"data":"inCount"},
					{"data":"outCount"},
					{"data":"turnover"}
			]);
		 	$("#detailModal").modal("show");
		},
		getFactoryTurnover:function(){
			var deptId=$("#productionDepartment option:selected").val();
			ajaxUtil.ajaxRuquestNoQuan('workshop/getFactoryTurnover',{"deptId":deptId},function(data){
				var xx=0;
				for(var i=0;i<data.length;i++){
					xx=xx+data[i].turnover;
				}
                $("#factoryTurnoverCount").text(xx);
			});
		},
		getMachining:function(workShop){
			ajaxUtil.ajaxRuquestNoQuan("workshop/getMachining",{"workShop":workShop},function(data){
				var html="<option value=''>请选择</option>";
				for(var i=0;i<data.length;i++){
					html+="<option value='"+data[i]+"'>"+data[i]+"</option>";
				}
				$("#Machining").html(html);
			});
		}
}


setInterval ("page.timesDo()", 900000);//十五分钟更新一次数据